package sanshui.system.trade.factory.timeManager;

import sanshui.system.trade.model.TradeTimeDriverModel;
import sanshui.system.trade.util.CommonTimeUtil;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

public abstract class TimeManager {
    private PriorityQueue<LocalDateTime> eventQueue = new PriorityQueue<>();

    // 最近一次的时间戳
    public LocalDateTime timestampCurrent;

    // 1m,5m,1d
    public String frequency;

    //在实时系统时，这个时间只是用来取得历史数据，方便指标计算
    public LocalDateTime startTime;
    public LocalDateTime endTime;

    public TimeManager(String frequency, LocalDateTime startTime, LocalDateTime endTime) {
        this.frequency = frequency;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public void loadTimestamp(LocalDateTime time){
        eventQueue.add(time);
    }

    protected void loadTimestamp(List<LocalDateTime> times){
        eventQueue.addAll(times);
    }

    /**
     * 取下一个时间
     * */
    public boolean hasNext(){
        return eventQueue.peek() != null;
    }

    public LocalDateTime next(){
        return eventQueue.peek();
    }

    public LocalDateTime tryNextTime(){
        return eventQueue.peek();
    }

    public void pollNext(){
        eventQueue.poll();
    }

    public void init(List<TradeTimeDriverModel> driverTimes){
        for (TradeTimeDriverModel driverTimeAgu : driverTimes) {
            LocalDateTime driverTime = driverTimeAgu.getDriverTime();
            this.loadTimestamp(driverTime);
        }
    }
}
